package com.wyp.design.pattern.creational.simplefactory;

/**
 * 简单工厂违反了开闭原则，可以使用反射进一步防止修改本类。
 *
 * @author: yipeng.wang01@hand-china.com-25490
 * @TIME: 2019/12/26/15:47
 **/
public class VideoFactory {

//    public Video createVideo(String name) {
////        Video video = null;
////        if (name.equals("java")) {
////            video = new JavaVideo();
////        } else if (name.equals("python")) {
////            video = new PythonVideo();
////        }
////        return video;
////    }

//    使用反射防止修改本类--开闭原则

    public Video createVideo(Class c) {
        Video video = null;

        try {
            video= (Video) Class.forName(c.getName()).newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        return video;
    }
}
